/*
 * .vue文件快速生成脚本
 */

const fs = require('fs')
const path = require('path')
const basePath = path.resolve(__dirname, '../src')

//文件名
const kebabCaseName = process.argv[2]
//路径
const pathName = process.argv[3]

if (!kebabCaseName || kebabCaseName.length < 1 || !kebabCaseName.includes('-')) {
  console.log('【错误】 像这样运行命令: npm run tpl your-page-name(usded kebab-case)  path(可选) ')
  process.exit(0)
}

const pascalCaseName = [...(kebabCaseName.toLowerCase().split(/-|_|\s/g))].map(item => item.substring(0, 1).toUpperCase() + item.substring(1)).join().replace(/,/g, '')
const rootClassName = [...(kebabCaseName.toLowerCase().split(/-|_|\s/g))].map( (item,index) =>  index==0?item:item.substring(0, 1).toUpperCase() + item.substring(1)).join().replace(/,/g, '')

/**
 * vue 页面模版
 */
const VueTep = `
<template>
  <div class="${rootClassName}">
  </div>
</template>

<script>
/**
 *  ${pascalCaseName}
 */
import {mapState, mapGetters, mapActions, mapMutations} from 'vuex'
import EventType from '@/js/eventType.js'

export default {
    name: "${pascalCaseName}",
    components: {},
    props: {},
    data() {
        return {
        }
    },
    computed: {
        ...mapState({
            Properties: state => state.app.Properties,
        }),
        ...mapGetters({
        }),
    },
    created() {
    },
    mounted() {
        this.addAllListener();
    },
    updated() {},
    methods: {
        ...mapMutations({
        }),
        addAllListener() {
            let that = this;
            this.$bus.$on(EventType.windowresize, (e) => {
            });
        },
    },
    watch: {
    },
    beforeDestroy() {
    }
}
</script>

<style lang="postcss" scoped>
 .${rootClassName}{
    
 }
</style>
`

const dir = `${basePath}\\${pathName?pathName:''}\\`
console.log(`目录：${dir}`);
if(pathName){
  if(!fs.existsSync(dir)){
    fs.mkdirSync(dir,true)
  }
}
process.chdir(dir)
fs.writeFileSync(`${pascalCaseName}.vue`, VueTep)
process.exit(0)